<template>
  <div>
    <!-- 渲染举报信息列表 -->
    <el-card>
      <!-- <el-card v-if="count == 0">暂无数据</el-card> -->
      <el-card>举报处理</el-card>
      <!-- eslint-disable-next-line vue/no-use-v-if-with-v-for -->
      <el-card v-for="(item,index) in userList" v-if="item.complain === 1 && item.status === 1 && item.message !== '无'" :key="item.id">
        <el-tag style="margin-left: -10px;">{{ index }}</el-tag>
        <el-tag type="danger">被举报者:{{ item.username }}</el-tag>
        <el-tag type="success">举报者:{{ item.complainer }}</el-tag>
        <el-tag type="warning" style="width:200">举报原因:{{ item.cause }}</el-tag>
        <el-button type="danger" style="float: right;" @click="ban(item.id)">禁用该用户</el-button>
        <el-button type="warning" style="float: right; margin-right: 10px;" @click="warning(item.id)">给予警告</el-button>
        <el-button type="success" style="float: right;" @click="pass(item.id)">无违规</el-button>
      </el-card>
    </el-card>

    <!-- 渲染黑名单列表 -->
    <el-button type="primary" @click="show">查看黑名单用户</el-button>
    <el-card v-show="isshow" class="ban">
      <el-card style="background-color: #f1f1f1">黑名单用户</el-card>
      <!-- eslint-disable-next-line vue/no-use-v-if-with-v-for -->
      <el-card v-for="(item,index) in userList" v-if=" item.status === 0" :key="item.id" style="background-color: #f1f1f1">
        <el-tag style="margin-left: -10px;">{{ index }}</el-tag>
        <el-tag type="danger">被举报者:{{ item.username }}</el-tag>
        <el-tag type="success">举报者:{{ item.complainer }}</el-tag>
        <el-tag type="warning" style="width:200">举报原因:{{ item.cause }}</el-tag>
        <el-button type="success" style="float: right;" @click="ban(item.id)">取消禁用该用户</el-button>
      </el-card>
    </el-card>

  </div>
</template>

<script>
import userApi from '@/api/userManage'
import userRoleApi from '@/api/userRoleManage'
// import goodsApi from '@/api/goodsManage'
export default {
  data() {
    return {
      userList: [],
      userRoleList: [],
      userFrom: {},
      isshow: false
    }
  },
  created() {
    this.getAllUser()
  },
  methods: {
    warning(id) {
      this.$confirm(`是否给予警告处理 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '警告处理!'
        })
        userApi.getUserById(id).then(response => {
          this.userFrom = response.data
          this.$set(this.userFrom, 'message', '经过核实，存在违规行为，此次给予警告处理，严重者禁用账号！')
          userApi.updateUser(this.userFrom)
          this.getAllUser()
          window.location.reload()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消处理'
        })
      })
    },
    ban(id) {
      this.$confirm(`是否执行此操作 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '成功执行!'
        })
        userRoleApi.getUserRoleListByUserId(id).then(response => {
          this.userRoleList = response.data
          // 禁用id为4
          if (this.userRoleList[0].roleId === 4) {
            this.$set(this.userRoleList[0], 'roleId', 3)
          } else {
            this.$set(this.userRoleList[0], 'roleId', 4)
          }
          userRoleApi.updateUserRole(this.userRoleList[0])
          userApi.getUserById(id).then(response => {
            this.userFrom = response.data
            if (this.userFrom.status === 1) {
              this.$set(this.userFrom, 'status', 0)
              this.$set(this.userFrom, 'message', '经过核实，该用户确实违规，目前已被禁用')
            } else {
              this.$set(this.userFrom, 'status', 1)
              this.$set(this.userFrom, 'message', '经过核实，该用户无违规，账号可以正常使用')
            }
            userApi.updateUser(this.userFrom)
            this.getAllUser()
            window.location.reload()
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消处理'
        })
      })
    },
    pass(id) {
      this.$confirm(`是否无违规放行 ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '无违规放行!'
        })
        userApi.getUserById(id).then(response => {
          this.userFrom = response.data
          this.$set(this.userFrom, 'complain', 0)
          this.$set(this.userFrom, 'message', '经过核实，用户无违规')
          userApi.updateUser(this.userFrom)
          this.getAllUser()
          window.location.reload()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消处理'
        })
      })
    },
    getAllUser() {
      userApi.getAllUser().then(response => {
        this.userList = response.data
      })
    },
    show() {
      this.isshow = !this.isshow
    }
  }
}
</script>

<style scoped>
.ban{
  background-color: #f1f1f1;
}
</style>
